package cn.itcast.xuesheng.dao.impl;

import cn.itcast.xuesheng.dao.TopicDao;
import cn.itcast.xuesheng.domain.Topic;
import cn.itcast.xuesheng.utils.JdbcUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Repository("topicDao")
public class TopicDaoImpl implements TopicDao {

   @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int findTotalcount(int cid,String title) {
        String sql="SELECT COUNT(*) FROM topic WHERE 1=1 ";
        StringBuilder sb=new StringBuilder(sql);
        List params=new ArrayList();
        if (cid!=0) {
            sb.append(" and cid = ? ");
            params.add(cid);
        }
        if (title!=null&&title.length()>0){
            sb.append(" and title like ? ");
            params.add("%"+title+"%");
        }
        sql=sb.toString();
        return jdbcTemplate.queryForObject(sql,Integer.class,params.toArray());
    }

    @Override
    public List<Topic> findpage(int cid, int start, int pageSize, String title) {
        String sql="SELECT * FROM topic WHERE 1 = 1 ";
        StringBuilder sb=new StringBuilder(sql);
        List params=new ArrayList();
        if (cid!=0) {
            sb.append(" and cid = ? ");
            params.add(cid);
        }
        if (title!=null&&title.length()>0){
            sb.append(" and title like ? ");
            params.add("%"+title+"%");
        }
        sb.append(" limit ? , ? ");
        sql=sb.toString();
        params.add(start);
        params.add(pageSize);

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class),params.toArray());
    }

    @Override
    public int findTotalcountbyuser(String username ) {
        String sql="SELECT COUNT(*) FROM topic WHERE username=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,username);
    }

    @Override
    public List<Topic> findpagebyuser(String username, int start, int pageSize) {
        String sql="SELECT * FROM topic WHERE username=? limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class),username,start,pageSize);
    }

    @Override
    public void update(int num,int tid) {
        String sql="UPDATE topic set thumbup=? WHERE tid=?";
        System.out.println(num);
        jdbcTemplate.update(sql,num,tid);
    }

    @Override
    public List<Topic> queryforxue() {
        String sql="SELECT * FROM topic WHERE cid=1 ORDER BY thumbup DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryforxue2() {
        String sql="SELECT * FROM topic WHERE cid=1 ORDER BY date DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryfortu() {
        String sql="SELECT * FROM topic WHERE cid=2 ORDER BY thumbup DESC";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryfortu2() {
        String sql="SELECT * FROM topic WHERE cid=2 ORDER BY date DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryfortnanv() {
        String sql="SELECT * FROM topic WHERE cid=3 ORDER BY date DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryforimg() {
        String sql="SELECT * FROM topic WHERE cid=4 ORDER BY thumbup DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryforxue3() {
        String sql="SELECT * FROM topic WHERE cid=5 ORDER BY date DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public List<Topic> queryforx4() {
        String sql="SELECT * FROM topic WHERE cid=6 ORDER BY date DESC ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Topic>(Topic.class));
    }

    @Override
    public void insertcontext(String username,String title,String context,int cid,String cname,String date,String imgcontext) {
        String sql="insert into topic (cid,cname,username,title,context,date,imgcontext) value(?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,cid,cname,username,title,context,date,imgcontext);
    }
}
